<template>
  <!-- 学科 -->
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <el-form :inline="true" size="small" :model="page">
          <el-form-item label="学科名称">
            <el-input v-model="page.subjectName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="qingChu">清除</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="12">
        <el-row type="flex" justify="end" :span="8">
          <el-button
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="addPermission"
            >添加权限</el-button
          >
        </el-row>
      </el-col>
    </el-row>
    <el-alert
      v-if="arleText !== ''"
      :title="arleText"
      type="info"
      show-icon
      :closable="false"
    ></el-alert>
    <el-table :data="list" style="width: 100%">
      <el-table-column label="序号" width="50" type="index"></el-table-column>
      <el-table-column label="学科名称" prop="subjectName"></el-table-column>
      <el-table-column label="创建者" prop="username"></el-table-column>
      <el-table-column label="创建日期" width="180" prop="addDate">
        <template v-slot="{ row }">
          {{ row.addDate | formatDate }}
        </template>
      </el-table-column>
      <el-table-column
        label="前台是否显示"
        prop="isFrontDisplay"
      ></el-table-column>
      <el-table-column
        label="二级目录"
        prop="twoLevelDirectory"
      ></el-table-column>
      <el-table-column label="标签" prop="tags"></el-table-column>
      <el-table-column label="题目数量" prop="totals"></el-table-column>
      <el-table-column label="操作" width="240">
        <template v-slot="{ row }">
          <el-button type="text" @click="xuekeFenlei(row)">学科分类</el-button>
          <el-button type="text" @click="xuekebiaoqian(row)"
            >学科标签</el-button
          >
          <el-button type="text" @click="editPermission(row.id)"
            >修改</el-button
          >
          <el-button type="text" @click="deleteDa(row.id)">删除</el-button>
        </template>
      </el-table-column>
      <hr />
    </el-table>
    <el-row type="flex" justify="end" style="height: 60px" align="middle">
      <el-pagination
        :page-sizes="[10, 15, 20, 30]"
        :page-size="page.pagesize"
        :current-page="page.page"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="sizeChange"
      >
      </el-pagination>
    </el-row>
    <!-- 编辑弹层 -->
    <el-dialog
      v-if="showDialog"
      :visible="showDialog"
      :title="title"
      width="35%"
      @close="btnCencal"
    >
      <!-- 表单 -->
      <el-form ref="permForm" :model="formDa" :rules="rules" label-width="80px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input
            v-model="formDa.subjectName"
            style="width:100%"
            placeholder="请输入学科名称"
          />
        </el-form-item>
        <el-form-item label="是否开启">
          <el-switch
            v-model="formDa.isFrontDisplay"
            :inactive-value="0"
            active-value="1"
            style="60%"
          />
        </el-form-item>
      </el-form>
      <!-- 确定和取消 -->
      <el-row type="flex" justify="end">
        <el-col :span="6">
          <el-button size="small" @click="btnCencal">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  meunDate,
  deleteDate,
  adddIscipline,
  disciPline,
  courseDetails
} from '@/api/subject'
import { formatDate } from '@/utils/formdata'
import { getDisciplineList } from '@/api/user'
export default {
  name: 'XueKe',
  filters: {
    formatDate(item) {
      const date = new Date(item)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  data() {
    return {
      arleText: '',
      total: null,
      list: [],
      showDialog: false,
      formDa: {
        subjectName: '',
        isFrontDisplay: 1
      },
      page: {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸 // 学科名称
        isFrontDisplay: '1',
        subjectName: ''
      },
      rules: {
        subjectName: [
          { required: true, message: '学科名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      if (this.page.id) {
        return '修改学科'
      } else {
        return '新增学科'
      }
    }
  },
  created() {
    this.meundate()
  },
  methods: {
    async meundate() {
      console.log(12)
      const { data } = await meunDate(this.page)
      this.list = data.items
      this.total = data.counts
      this.arleText = `共有${this.total}条`
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.meundate()
    },
    sizeChange(pagesize) {
      this.page.pagesize = pagesize
      this.meundate()
    },
    deleteDa(id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?')
        .then(() => {
          return deleteDate(id)
        })
        .then(() => {
          this.$message.success('删除成功')
          this.meundate()
        })
    },
    addPermission() {
      this.showDialog = true
    },
    btnOk() {
      this.$refs.permForm
        .validate()
        .then(() => {
          // 校验成功
          if (this.formDa.id) {
            // 编辑
            return disciPline(this.formDa)
          }
          return adddIscipline(this.formDa)
        })
        .then(() => {
          // 添加成功
          this.$message.success('操作成功')
          this.meundate() // 拉取数据
          console.log('是否执行')
          this.showDialog = false
          this.formDa = {
            subjectName: '',
            isFrontDisplay: 1
          }
        })
    },
    // 取消
    btnCencal() {
      this.formDa = {
        subjectName: '',
        isFrontDisplay: 1
      }
      this.$refs.permForm.resetFields()
      this.showDialog = false
    },
    async editPermission(id) {
      this.showDialog = true
      const { data } = await courseDetails(id)
      console.log(data)
      this.formDa = data
    },
    qingChu() {
      this.page.subjectName = ''
      this.meundate()
    },
    // 搜索
    search() {
      this.meundate()
      this.page = {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        subjectName: '', // 学科名称
        isFrontDisplay: '0'
      }
    },
    // 到学科分类
    async xuekeFenlei(params) {
      const { data } = await getDisciplineList()
      const resList = data.filter(item => item.label === params.subjectName)
      const subjectID = resList[0].value
      this.$router.push({
        path: '/catalogue',
        query: {
          id: subjectID,
          params: params.subjectName
        }
      })
    },
    // 到学科标签
    async xuekebiaoqian(params) {
      const { data } = await getDisciplineList()
      const resList = data.filter(item => item.label === params.subjectName)
      const subjectID = resList[0].value
      this.$router.push({
        path: '/label',
        query: {
          id: subjectID,
          params: params.subjectName
        }
      })
    }
  }
}
</script>
<style lang="scss">
.el-dialog__header {
  background-color: #409eff;
  .el-dialog__title {
    color: #fff;
  }
  .el-dialog__close {
    color: #fff;
  }
}
</style>
